﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using LINQPad.OpenAccess.Helpers;
using LINQPad.OpenAccess.Infrastructure;
using LINQPad.OpenAccess.Metadata.Reflection.Adapters;

namespace LINQPad.OpenAccess.Metadata.Reflection.Locators
{
    /// <summary>
    ///   <see cref="IContextTypeLocator" />.
    /// </summary>
    public class ContextTypeLocator : IContextTypeLocator
    {
        #region Private

        #region - Vars

        [DebuggerBrowsable( DebuggerBrowsableState.Never )]
        private readonly IContextTypeInfoAdapter _contextTypeInfoAdapter;
        [DebuggerBrowsable( DebuggerBrowsableState.Never )]
        private readonly IReflectionService _reflectionService;

        #endregion

        #endregion

        #region Public

        #region - Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ContextTypeLocator" /> class.
        /// </summary>
        /// <param name="contextTypeInfoAdapter">The context type info adapter.</param>
        /// <param name="reflectionService">The reflection service.</param>
        public ContextTypeLocator( IContextTypeInfoAdapter contextTypeInfoAdapter, IReflectionService reflectionService )
        {
            Guard.ArgumentNotNull( contextTypeInfoAdapter, "contextTypeInfoAdapter" );
            Guard.ArgumentNotNull( reflectionService, "reflectionService" );
            
            this._contextTypeInfoAdapter = contextTypeInfoAdapter;
            this._reflectionService = reflectionService;
        }

        #endregion

        #endregion

        #region IContextTypeLocator Members

        /// <summary>
        /// Gets the metadata source info from the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <returns>
        /// The metadata source info.
        /// </returns>
        public IEnumerable<IContextTypeInfo> GetFromAssembly( Assembly assembly )
        {
            Guard.ArgumentNotNull( assembly, "assembly" );

            var adapter = this._contextTypeInfoAdapter;
            return this._reflectionService
                        .GetContextTypesFromAssembly( assembly )
                        .Select( adapter.Adapt );
        }

        #endregion

        #region IContextTypeLocator Members


        #endregion
    }
}